import java.util.ArrayList;

class Hand {
	private ArrayList<Card> hand;
	private static final int HAND_SIZE = 4;
	private static final int NUM_OF_RANKS = 13;
	Hand() {
		hand = new ArrayList<Card>(HAND_SIZE);
	}
	
	
	Card getCard(final int i) throws BadugiException {
	    if (i >= hand.size()) {
	        throw new BadugiException("No such index");
	    } else {
	        return hand.get(i);
	    }
	}
	
	boolean findCard(final int rank, final String suit) {
	    Card card;
	    for (int i = 0; i < hand.size(); i++) {
	        card = hand.get(i);
	        if (card.getRank() == rank && card.getSuit() == suit) {
	            return true;
	        }
	    }
	    return false;
	}
	
	void cardRemove(final int i, final Deck deck) {
		deck.addToUsed(hand.get(i));
		hand.remove(i);
		
	}
	void cardPick(final Deck deck) {
		hand.add(deck.drawCard());
	}
	
	int findRankIndex(final int rank) {  // ma znalezc!
	    int result = 0;
	    for (int i = 0; i < HAND_SIZE; i++) {
	        if (hand.get(i).getRank() == rank) {
	            result = i;
	        }
	    }
	    return result;
	}
	
	void cardAdd(final int rank, final String suit) {
	    Card card;
        try {
            card = new Card(rank, suit);
        } catch (BadugiException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return;
        }
	    hand.add(card);
	}
	int[] getRanks() {
	    int[] result = new int[NUM_OF_RANKS];
	    for (int i = 0; i < HAND_SIZE; i++) {
            result[hand.get(i).getRank()]++;
        }
	    return result;
	}
	public String toString() {
		String result = "";
		for (int i = 0; i < hand.size(); i++) {
			result += hand.get(i).toString();
			result += " ";
		}
		return result;
	}
	
	private int getSize() {
		if (hand == null) {
            return 0;
        } else {
            return hand.size();
        }
	}
	
	int getHandRank() throws BadugiException {
		int suits[] = new int[4];		// S, C, D, H
		int ranks[] = new int[13];
		for(int i = 0; i < HAND_SIZE; i++) {
			if(hand.get(i).getSuit() == "S")
				suits[0]++;
			if(hand.get(i).getSuit() == "C")
				suits[1]++;
			if(hand.get(i).getSuit() == "D")
				suits[2]++;
			if(hand.get(i).getSuit() == "H")
				suits[3]++;
			ranks[hand.get(i).getRank()]++;
		}
		int diffsuits = 0;
		int diffranks = 0;
		for (int i = 0; i < 4; i++) {
			if(suits[i] > 0)
				diffsuits++;
		}
		for (int i = 0; i < 13; i++) {
			if(ranks[i] > 0)
				diffranks++;
		}
		if(diffsuits == 4)
			return diffranks;
		else if(diffsuits == 3) {
			if(diffranks == 4)
				return 3;
			else if(diffranks == 3) {
				int badrank = -1;
				for(int i = 0; i < 13; i++) {
					if(ranks[i]==2)
						badrank = i;
				}
				if(badrank == -1) throw new BadugiException("Something wrong happened");
				for(int i = 0; i < HAND_SIZE; i++) {
					if(hand.get(i).getRank() == badrank) {
						if(hand.get(i).getSuit() == "S" && suits[0] == 2)
							return 3;
						if(hand.get(i).getSuit() == "C" && suits[1] == 2)
							return 3;
						if(hand.get(i).getSuit() == "D" && suits[2] == 2)
							return 3;
						if(hand.get(i).getSuit() == "H" && suits[3] == 2)
							return 3;
					}
				}
				return 2;
			}
			else if(diffranks == 2)
				return 2;
			else throw new BadugiException("Something wrong happened");			
		}
		else if(diffsuits == 2) {
			return 2;
		}
		else if(diffsuits == 1) {
			return 1;
		}
		else throw new BadugiException("Something wrong happened");
	}
	private int findMaxIndex(final int[] array) {
		int max = 0;
		int result = 0;
		for (int i = 0; i < array.length; i++) {
			if (array[i] > max) {
				max = array[i];
				result = i;
			}
		}
		return result;
	}
	private int kickersCmp(final int[] thisranks, final int[] thatranks) {
		int result = 0;
		for (int i = NUM_OF_RANKS - 1; i >= 0; i--) {
			if (thisranks[i] > thatranks[i]) {
				result = -1;
				break;
			} else if (thisranks[i] < thatranks[i]) {
				result = 1;
				break;
			}
		}
		return result;
	}
	int compareTo(final Hand that) {
		int result;
		if (that == null) {
            return 1;
        }
		int thisrank, thatrank;
		try {
			thisrank = this.getHandRank();
			thatrank = that.getHandRank();
		} catch (BadugiException e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
			return 0;
		}
		if (thisrank > thatrank) {
            result = 1;
        } else if (thisrank == thatrank) {
			int[] thisranks = new int[NUM_OF_RANKS];
			int[] thatranks = new int[NUM_OF_RANKS];
			for (int i = 0; i < HAND_SIZE; i++) {
				thisranks[this.hand.get(i).getRank()]++;
				thatranks[that.hand.get(i).getRank()]++;
			}
			result = kickersCmp(thisranks,thatranks);
		} else {
            result = -1;
        }
		return result;
	}
}
